﻿using System;
using TidyBlog.Data.Mapping;

namespace TidyBlog.Data
{
    /// <summary>
    /// WHERE子句条件
    /// </summary>
    public class WhereCondition
    {
        /// <summary>
        /// 获取或设置列
        /// </summary>
        public string Column { get; set; }
        /// <summary>
        /// 获取或设置操作符，默认为EQUAL
        /// </summary>
        public WhereOperator Operator { get; set; }
        /// <summary>
        /// 获取或设置值
        /// </summary>
        public string Value { get; set; }
        /// <summary>
        /// 获取或设置值是否参数形式，默认为True
        /// </summary>
        public bool IsParamValue { get; set; }
        /// <summary>
        /// 获取或设置与前面条件的连接运算符，默认为AND
        /// </summary>
        public LogicalOperator ConnectionOperator { get; set; }


        public WhereCondition()
        {
            SetDefaultValue();
        }
        /// <summary>
        /// 根据数据列特性初始化，列和值均为列名
        /// </summary>
        /// <param name="colAttr">数据列特性</param>
        public WhereCondition(ColumnAttribute colAttr)
        {
            Column = colAttr.Name;
            Value = colAttr.Name;

            SetDefaultValue();
        }
        /// <summary>
        /// 根据列名初始化，列和值均为列名
        /// </summary>
        /// <param name="colName">列名</param>
        public WhereCondition(string colName)
        {
            Column = colName;
            Value = colName;

            SetDefaultValue();
        }
        /// <summary>
        /// 根据列和值初始化，值为非参数
        /// </summary>
        /// <param name="column">列名</param>
        /// <param name="value">值</param>
        public WhereCondition(string column, string value)
        {
            Column = column;
            Value = value;

            SetDefaultValue();

            IsParamValue = false;
        }


        /// <summary>
        /// 设置默认值
        /// </summary>
        protected void SetDefaultValue()
        {
            IsParamValue = true;
            Operator = WhereOperator.EQUAL;
            ConnectionOperator = LogicalOperator.AND;
        }
    }
}